﻿Imports System.Data
Imports System.Windows.Forms
Imports Microsoft.Office.Interop.Word
Imports Chiroredactie2007.My.Resources

Public Class WoordentellingVenster

    Private Const KLEINEWOORDENFILTER As String = "len(Woord) > 3"
    Private Const CHIROFILTER As String = "Woord LIKE '%chiro%'"
    Private Const PASSIVITISFILTER As String = "Woord IN ('worden', 'geworden', 'word', 'wordt', 'werd', 'werden')"
    ' OPM: 'kan' kan ook zelfstandig naamwoord zijn
    Private Const TWIJFELSTIJLFILTER As String = "Woord IN ('kunnen', 'kan', 'kunt', 'kun', 'gekund', 'eventueel', 'misschien', 'mogelijk')"

    Private ReadOnly selectie As Selection = Globals.ThisAddIn.Application.Selection

    Private tabel As Data.DataTable
    Private dv As DataView

    Private Sub WoordentellingVenster_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        TellingUitvoeren()
        TopMost = True
        KleineWoordenRadioButton.Checked = True
    End Sub

    Private Sub KleineWoordenRadioButton_CheckedChanged(ByVal sender As System.Object, ByVal e As EventArgs) Handles KleineWoordenRadioButton.CheckedChanged
        If dv IsNot Nothing AndAlso KleineWoordenRadioButton.Checked Then
            dv.RowFilter = KLEINEWOORDENFILTER
        End If
    End Sub

    Private Sub ChiroRadioButton_CheckedChanged(ByVal sender As System.Object, ByVal e As EventArgs) Handles ChiroRadioButton.CheckedChanged
        If dv IsNot Nothing AndAlso ChiroRadioButton.Checked Then
            dv.RowFilter = CHIROFILTER
        End If
    End Sub

    Private Sub WordenRadioButton_CheckedChanged(ByVal sender As System.Object, ByVal e As EventArgs) Handles WordenRadioButton.CheckedChanged
        If dv IsNot Nothing AndAlso WordenRadioButton.Checked Then
            dv.RowFilter = PASSIVITISFILTER
        End If
    End Sub

    Private Sub TwijfelstijlRadioButton_CheckedChanged(ByVal sender As System.Object, ByVal e As EventArgs) Handles TwijfelstijlRadioButton.CheckedChanged
        If dv IsNot Nothing AndAlso TwijfelstijlRadioButton.Checked Then
            dv.RowFilter = TWIJFELSTIJLFILTER
        End If
    End Sub

    Private Sub WoordenDataGridView_SelectionChanged(ByVal sender As System.Object, ByVal e As EventArgs) Handles DataGridView1.SelectionChanged
        selectie.Find.ClearHitHighlight()

        If DataGridView1.SelectedRows.Count = 1 Then
            selectie.Find.Wrap = WdFindWrap.wdFindContinue
            selectie.Find.HitHighlight(DataGridView1.SelectedRows(0).Cells(0).Value)
        End If
    End Sub

    Private Sub HertellenButton_Click(ByVal sender As System.Object, ByVal e As EventArgs) Handles HertellenButton.Click
        TellingUitvoeren()
    End Sub

    Private Sub Afsluiten(ByVal sender As System.Object, ByVal e As EventArgs) Handles Me.Closing
        selectie.Find.ClearHitHighlight()
    End Sub

    Private Sub TellingUitvoeren()
        Cursor.Current = Cursors.WaitCursor

        tabel = Eindredactie.WoordenTellen()

        ' Niet meetellen: lidwoorden, voorzetsels, voornaamwoorden, voegwoorden
        ' DOC: http://nl.wikipedia.org/wiki/Lijst_van_Nederlandse_voorzetsels

        dv = New DataView(tabel, String.Empty, "Aantal DESC", DataViewRowState.CurrentRows)

        DataGridView1.DataSource = dv
        Label1.Text = WoordentellingConcatString & Globals.ThisAddIn.Application.ActiveDocument.Words.Count

        Cursor.Current = Cursors.Default
    End Sub

End Class